package com.example.meetingroomnew.controll;

import com.example.meetingroomnew.domain.LoginClass;
import com.example.meetingroomnew.domain.User;
import com.example.meetingroomnew.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@CrossOrigin(origins = {"*","null"})
@RestController
@RequestMapping
public class UserControll {
    @Autowired
    UserService userService;

//    判断登陆
    @PostMapping("/login")
    public LoginClass login(@RequestBody User newuser){
        System.out.println(newuser);
        int id=newuser.getId();
        String password=newuser.getPassword();
        System.out.println(id+" "+password);
        User user = userService.selectByid(id);
        LoginClass loginClass=new LoginClass();
        password= password.trim();
        if(user.getPassword().equals(password)){

            loginClass.setError(LoginClass.SUCCESS);
            loginClass.setUser(user);
        }else {
            loginClass.setError(LoginClass.FAILED);
            loginClass.setUser(null);
        }
        return loginClass;

    }
    @PostMapping("/register")
    public User Register(@RequestBody User user){
        user.setType(User.CONSUMER);
        System.out.println("123");
        int id = userService.AddNewUser(user);
        System.out.println("id="+id);
        User user1=new User();
        user1.setId(id);
        return user1;
    }


}
